package service.Imp;


import Message.DTO.DishDTO;
import Message.DTO.DishPageQueryDTO;
import Message.PageResult;
import Message.entity.Dish;
import Message.entity.DishFlavor;
import Message.vo.DishVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import mapper.DishFlavorMapper;
import mapper.DishMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.DishManageService;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishManageServiceImp implements DishManageService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;


    @Override
    public DishVO SelectDish(Integer id) {

       return dishMapper.SelectDish(Long.valueOf(id));

    }

    @Override
    public PageResult DishSelectList(DishPageQueryDTO dishPageQueryDTO ) {

        // 1.设置分页参数
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
// 2.调用mapper的列表查询方法，强转成Page

    Page<DishVO> page =   dishMapper.DishSelectList(dishPageQueryDTO);
        // 3.封装PageResult对象并返回

        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public List<DishVO> DishCategoryId(Long categoryId,String name) {
        return dishMapper.DishCategoryId(categoryId,name);
    }

    /**
     *
     * 菜品起售 停售
     * @param status
     * @param id
     */
    @Override
    @Transactional //开启事务
    public void StatusDish(Integer status, Long id) {
        log.info("status={}",status,id);
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.Update(dish);
        // 如果是停售操作，还需要将包含当前菜品的套餐也停售
//      if(status.equals(StatusConstant.DISABLE)){
//          // 联表查询setmeal、dish、setmeal_dish  写套餐再补充
//
//      }


    }

    @Override
    public void UpdateDish(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        // 1.修改菜品的基本信息，dish表
        dishMapper.Update(dish);

        // 2.修改口味列表信息，dish_flavor表
        // 由于口味数据可能增加、可能删除、还可能修改口味的值，涉及到增删改操作，所以先全部删除旧数据，再添加新数据

        dishFlavorMapper.deleteByDishId(dish.getId());

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if(flavors!=null&&flavors.size()>0){
            // 关联菜品id
            flavors.forEach(flavor->{
                flavor.setDishId(dish.getId());
            });

            dishFlavorMapper.insert(flavors);

        }

    }

    @Override
    public void InsertDish(DishDTO dishDTO) {
        // 1.构造菜品基本信息数据，将其存入dish表中
        Dish dish = new Dish();
        // 拷贝属性值
       BeanUtils.copyProperties(dishDTO,dish);
       

        // 调用mapper保存方法

          dishMapper.InsertDish(dish);
            

        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors!=null&&flavors.size()>0){
            // 2.1关联菜品id
            flavors.forEach(flavor -> {

                flavor.setDishId(dish.getId());

            });
            
            // 2.2 调用mapper保存方法,批量插入口味列表数据
                dishFlavorMapper.insert(flavors);
        }


    }

    @Transactional
    @Override
    public void delete(List<Long> ids) {
        // 3.删除菜品基本信息 dish表
        dishMapper.deleteBatch(ids);

        // 4.删除菜品口味列表信息 dish_flavor表
        dishFlavorMapper.deleteBatch(ids);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.listBy(dish);
        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.selectByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


}
